package com.heima.controller;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.heima.common.Result;
import com.heima.pojo.dto.SkuDto;
import com.heima.pojo.dto.SkuPageDto;
import com.heima.pojo.vo.SkuPageVo;
import com.heima.service.SkuService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vm-service/sku")
@Slf4j
public class SkuController {

    @Autowired
    private SkuService skuService;


    /**
     * 新增商品
     */
    @PostMapping
    @ApiOperation("新增商品")
    public Result addSku(@RequestBody SkuDto dto){
        log.info("接收到的商品对象为:{}",dto);
        skuService.addSku(dto);
        return Result.success();
    }

    /**
     * 商品搜索
     */
    @GetMapping("/search")
    @ApiOperation("商品搜索")
    public SkuPageVo pageQuery(SkuPageDto dto){
        log.info("接收到的参数为:{}",dto);
        return skuService.pageQuery(dto);
    }

    /**
     * 修改商品
     */
    @PutMapping("/{skuId}")
    @ApiOperation("修改商品")
    public Result update(@PathVariable @JsonSerialize(using = ToStringSerializer.class) String skuId, @RequestBody SkuDto dto){
        log.info("接收到的参数为:{},{}",skuId,dto);
        long l = Long.parseLong(skuId);
        dto.setSkuId(l);
        skuService.update(dto);
        return Result.success();
    }


}
